'use strict'
var info = require('./info');
 info();

var table = require('./table')
var Crawler = require("crawler");
for (let index=0;index<=250;index+=25) {
    var URL ='https://book.douban.com/top250?start='+index
var c = new Crawler({
    maxConnections: 5000,
    // This will be called for each crawled page
    callback: function (error, res, done) {
        if (error) {
            console.log(error);
        } else {
            var $ = res.$;
            var list = $('#wrapper .article table').toArray();
            list.forEach(element => {
                var txt1 = $(element).find('.pl2').text().trim().replace(/[ \n\r]/g, "");
                var txt2 = $(element).find('.pl').text().trim().replace(/\s*/g, "");
                var txt3 = $(element).find('.quote').text().trim().replace(/\s*/g, "");

                var a = txt2.split('/').slice(0, 1).toString();
                table.create({ title: txt1, who: a, intro: txt3 })
                console.log(txt1);
                console.log(txt2);
                console.log(txt3);
            });
            // $ is Cheerio by default
            //a lean implementation of core jQuery designed specifically for the server

        }
        done();
    }
});

// Queue just one URL, with default callback
// c.queue('https://book.douban.com/latest');

// var Crawler = require("crawler");
//  var fs =require('fs');
//  var request =require('request');

// var c = new Crawler({
//     maxConnections : 10,
//     // This will be called for each crawled page
//     callback : function (error, res, done) {
//         if(error){
//             console.log(error);
//         }else{
//             var $ = res.$;
//             var list=$('#wrapper .article ul li').toArray();
//             list.forEach(element => {
//                 var img =$(element).find('img').attr('src');

//                 var i = img.split('/').slice(7,8).toString();
//               request(img).pipe(fs.createWriteStream('./img/'/*路径名*/ + i + ".jpg"))

//                 console.log(img);


//             });
//             // $ is Cheerio by default
//             //a lean implementation of core jQuery designed specifically for the server         
//         }
//         done();
//     }
// });
// Queue just one URL, with default callback
// var url=require('url');

// var a ='https://book.douban.com/top250?';
// var  hppt ='start=25'
// var newurl=url.resolve(a,hppt).toString();

// console.log(newurl)


c.queue(URL);


}